//
//  HMNavigationController.swift
//  Weibo22
//
//  Created by Apple on 16/11/30.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class HMNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置代理手势
        self.interactivePopGestureRecognizer?.delegate = self
        
        // Do any additional setup after loading the view.
    }

    //  重写push方法,监听push操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //  隐藏底部tabar操作地这个地方
        if viewControllers.count > 0 {
            //   隐藏底部tabar
            viewController.hidesBottomBarWhenPushed = true
        }
        
        
        super.pushViewController(viewController, animated: animated)
        
        if viewControllers.count > 1 {
            if viewControllers.count == 2 {
                //  获取导航栏里面的第一个子视图控制器的标题
                let title = viewControllers.first!.title!
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
                
                
                
            } else {
                //  统一标题改成返回
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
            }
            
            viewController.title = "当前显示的是第\(viewControllers.count)级控制器"
        }
        
    }
  
    
    @objc private func popAction() {
        
        self.popViewController(animated: true)
    
    }
    
    

}

//  MARK: -- UIGestureRecognizerDelegate
extension HMNavigationController: UIGestureRecognizerDelegate {
    //  是否处理这次点击的边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1 {
            //  如果是跟视图控制器不让去处理这个边缘手势
            return false
        }
        
        return true
        
        
    }

}





